<script setup lang="ts">
import { Search, Refresh } from "@element-plus/icons-vue";
import { reactive, useTemplateRef } from "vue";
import type { ElForm } from "element-plus";

const searchForm = reactive({
  name: "",
  realname: "",
  cellphone: "",
  enable: 1,
  createAt: ""
});
const formRef = useTemplateRef<InstanceType<typeof ElForm>>("formRef");

/**
 * 重置表单
 */
const resetForm = () => {
  // 使用该方法需要同时具有 model 和 prop 属性的对应
  formRef.value?.resetFields();
  emit("resetClick");
};

const emit = defineEmits(["queryClick", "resetClick"]);

const searchUser = () => {
  emit("queryClick", searchForm);
};
</script>

<template>
  <div class="user-search">
    <el-form :model="searchForm" ref="formRef">
      <el-row gutter="20">
        <el-col span="8" :lg="8" :sm="24" :md="12">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="searchForm.name" placeholder="请输入用户名" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col span="8" :lg="8" :sm="24" :md="12">
          <el-form-item label="真实姓名" prop="realname">
            <el-input
              v-model="searchForm.realname"
              placeholder="请输入真实姓名"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col span="8" :lg="8" :sm="24" :md="12">
          <el-form-item label="手机号" prop="cellphone">
            <el-input
              v-model="searchForm.cellphone"
              placeholder="请输入手机号"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col span="8" :lg="8" :sm="24" :md="12">
          <el-form-item label="状态" prop="enable" label-width="54">
            <el-select v-model="searchForm.enable" placeholder="请选择状态">
              <el-option label="启用" :value="1"></el-option>
              <el-option label="禁用" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col span="8" :lg="8" :sm="24" :md="12">
          <el-form-item label="创建时间" prop="createAt">
            <el-date-picker
              style="width: 100%"
              v-model="searchForm.createAt"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="btn-group">
        <el-button type="primary" :icon="Search" @click="searchUser">查询</el-button>
        <el-button :icon="Refresh" @click="resetForm">重置</el-button>
      </div>
    </el-form>
  </div>
</template>

<style scoped lang="less">
.user-search {
  background: #fff;
  padding: 20px;
  border-radius: 20px;

  .el-form {
    .btn-group {
      display: flex;
      justify-content: flex-end;

      .el-button {
        margin-left: 20px;
      }
    }
  }
}
</style>
